package com.actionsoft.bpms.server.fs.impl;


import com.actionsoft.sdk.local.SDK;
import com.actionsoft.sdk.local.api.LogAPI;
import com.actionsoft.sdk.local.api.Logger;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @Author:caipw
 * @Description:
 * @Date 2018/12/27
 */

public class FileHelper {
    private static Logger logger = LogAPI.getLogger(FileHelper.class);
    /**
     * 从网络Url中下载文件
     * @param urlStr
     * @throws IOException
     */
    public static HttpURLConnection downLoadFromUrl(String urlStr){
        SDK.getLogAPI().consoleInfo("获取到的下载链接===="+urlStr);
        HttpURLConnection conn = null;
        if (isConnect(urlStr)){
            try {
                URL url = new URL(urlStr);
                conn = (HttpURLConnection)url.openConnection();

                //设置超时间为3秒
                conn.setConnectTimeout(3*1000);
                //防止屏蔽程序抓取而返回403错误
                conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");


            } catch (IOException e) {
                e.printStackTrace();
                logger.error(e.getMessage(),e);
            }
        }else{
            System.out.println("url:"+urlStr+" download failed");
        }
        return conn;
    }

    /**
     * 功能：检测当前URL是否可连接或是否有效,
     * @param urlStr 指定URL网络地址
     * @return URL
     */
    public static synchronized boolean isConnect(String urlStr) {
        URL url;
        HttpURLConnection con;
        int state = -1;
        int counts = 0;
        boolean isOk = false;
        if (urlStr == null || urlStr.length() <= 0) {
            isOk=false;
        }
        try {
            url = new URL(urlStr);
            con = (HttpURLConnection) url.openConnection();
            state = con.getResponseCode();
            if (state == 200) {
                isOk=true;
            }
        } catch (Exception e) {
            e.printStackTrace();
            isOk=false;
        }
        return isOk;
    }

    /**
     * 从输入流中获取字节数组
     * @param inputStream
     * @return
     * @throws IOException
     */
    public static  byte[] readInputStream(InputStream inputStream) throws IOException {
        byte[] buffer = new byte[1024];
        int len = 0;
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        while((len = inputStream.read(buffer)) != -1) {
            bos.write(buffer, 0, len);
        }
        bos.close();
        return bos.toByteArray();
    }






    /**
     * 将时间转换为时间戳
     */
    public static long dateToStamp(String s){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = null;
        try {
            date = simpleDateFormat.parse(s);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        long ts = 0;
        if (date != null) {
            ts = date.getTime();
        }
        return ts;
    }


}
